class Solution {
public:
    int longestEqualSubarray(vector<int>& nums, int k)
    {
        //元素 起始点 终止点 当中的元素个数
        int n = nums.size();
        int ans = 0;
        vector<vector<int>> t(n + 1);
        for (int i = 0; i < n; i++)
            t[nums[i]].push_back(i - t[nums[i]].size());
        for (auto& a : t)
        {
            if (ans >= a.size()) continue;
            int right = 0;
            int left = 0;
            int sz = a.size();
            while (right < sz)
            {
                while (left <= right && a[right] - a[left] > k) left++;
                ans = ans > right - left + 1 ? ans : right - left + 1;
                right++;
            }
        }
        return ans;
    }
};